package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.RoomMapper;
import mianyang.zhangsan.com.pojo.BookOrder;
import mianyang.zhangsan.com.pojo.Room;
import mianyang.zhangsan.com.pojo.RoomCustom;
import mianyang.zhangsan.com.pojo.UploadFile;
import mianyang.zhangsan.com.service.RoomService;
import mianyang.zhangsan.com.utils.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("roomService")
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 分页查询所有房间信息
     * @param pageNo
     * @return
     */
    @Override
    public Page<Room> queryPages(int pageNo) {
        return roomMapper.selectPage(new Page<>(pageNo, 3), null);
    }

    /**
     * 根据房间号查询房间信息
     * @param room
     * @return
     */
    @Override
    public Room queryRoomInfo(String room) {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("roomNo", room);
        return roomMapper.selectOne(wrapper);
    }

    /**
     * 根据房间号查询房间信息
     * @param room
     * @return
     */
    @Override
    public Room queryRoomInfo(int room) {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("roomNo", room);
        return roomMapper.selectOne(wrapper);
    }

    /**
     * 根据条件分页查询房间信息
     * @param page
     * @param rows
     * @param room
     * @return
     */
    @Override
    public Page<Room> queryPageByWrapper(Integer page, Integer rows, Room room) {
        QueryWrapper<Room> wrapper = queryWrapper(room);
        return roomMapper.selectPage(new Page<>(page, rows), wrapper);
    }

    /**
     * 封装房间信息
     * @param roomType
     * @param item
     * @return
     */
    @Override
    public RoomCustom packing(String roomType, Room item) {
        RoomCustom roomCustom = new RoomCustom();
        roomCustom.setRoomNo(item.getRoomNo());
        roomCustom.setRoomDesc(item.getRoomDesc());
        roomCustom.setRoomPhoto(item.getRoomPhoto());
        roomCustom.setRoomPrice(item.getRoomPrice());
        if ("1".equals(item.getRoomState())){
            roomCustom.setRoomState("占用");
        }else{
            roomCustom.setRoomState("空闲");
        }
        roomCustom.setRoomTypeObj(roomType);
        roomCustom.setFloorNum(item.getFloor() + "楼");
        return roomCustom;
    }

    /**
     * 删除多个房间信息
     * @param roomNos
     */
    @Override
    public void deletes(String roomNos) {
        String[] rooms = roomNos.split(",");
        for (String room : rooms){
            deleteOne(room);
        }
    }

    /**
     * 添加房间信息
     * @param room
     */
    @Override
    public void addRoom(Room room) {
        roomMapper.insert(room);
    }

    /**
     * 删除房间信息
     * @param roomNo
     * @return
     */
    @Override
    public int deleteOne(String roomNo) {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("roomno", roomNo);
        int delete = roomMapper.delete(wrapper);
        return delete;
    }

    /**
     * 更新房间信息
     * @param room
     */
    @Override
    public void update(Room room) {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        if (room.getRoomNo() != null &&  "".equals(room.getRoomNo())){
            wrapper.eq("roomno", room.getRoomNo());
        }
        roomMapper.update(room, wrapper);
    }

    /**
     * 查询所有的房间信息
     * @return
     */
    @Override
    public List<Room> queryList() {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("roomstate", "0");
        return roomMapper.selectList(wrapper);
    }

    /**
     * 根据条件查询所有的房间信息
     * @param room
     * @return
     */
    @Override
    public List<Room> queryListByWrapper(Room room) {
        QueryWrapper<Room> wrapper = queryWrapper(room);
        return roomMapper.selectList(wrapper);
    }

    /**
     * 封装查询条件
     * @param room
     * @return
     */
    @Override
    public QueryWrapper<Room> queryWrapper(Room room) {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        if (room.getRoomNo() != null &&  !"".equals(room.getRoomNo())){
            wrapper.eq("roomno", room.getRoomNo());
        }
        if (room.getRoomType() != null){
            wrapper.eq("roomtypeobj", room.getRoomType());
        }
        if (room.getRoomPrice() != null){
            wrapper.lt("roomprice", room.getRoomPrice());
        }
        return wrapper;
    }
}
